/*******************************************************************************
TablesIII_VI.do

This file creates tables to show largest firm in town results using data created
in "Code\Setup\NETS_Largest_Firm_in_Town_Data.do". It regresses log total 
employment of the firm with the most plants in an industry-location pair on log 
population density and industry fixed effects, weighted by industry employment. 
Unless specified, results are produced using the average log firm size among 
firms in an industry-location that tie for having the most plants and using M=12.

Table III: baseline M=12, baseline M=48, firms with at least 100 plants, 
		   industries in which the largest firm has at least 100 plants
		 
Table VI:
1) baseline M=3, 6, 12, 24, 48
2) firms with at least X=10, 20, 50, 100 plants (M=12)
3) industries where the largest firm has at least X=10, 20, 50, 100 plants (M=12)
4) manufacturing, services, retail trade, FIRE (M=12)
5) additional results: excluding firm-location contribution to a firm's total
   employment, alternative methods to handling ties, using non-imputed data (M=12)

Last updated: 4/29/2021
*******************************************************************************/

version 15
cd "C:\Plants_in_Space"
set more off
set type double
set varabbrev off
graph set window fontface "Times New Roman" 

**************Table III: National size of the largest firm in town**************
// Baseline M=12
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_mean_of_ties_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableIII.tex", label replace tex(fragment) nocons ///
	ctitle("\textit{Baseline}", "\textit{$ M = 12$}", "$\hspace{2em}\ln L_{j^*(s)}\hspace{2em}$") addtext(SIC8 FE, Yes, M, 12)

preserve
	// Baseline M=48
	use "Data\Final\TablesIII_VI\largest_firm_in_town_M48_mean_of_ties_data.dta", clear
	rename log_pop_density_48 log_pop_density
	label variable log_pop_density "$\ln \mathcal{L}_{s}$"
	reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
	outreg2 using "Tables\TableIII.tex", label append tex(fragment) nocons ///
		ctitle("\textit{Baseline}", "\textit{$ M = 48$}", "$\hspace{2em}\ln L_{j^*(s)}\hspace{2em}$") addtext(SIC8 FE, Yes, M, 48)

	// Firms with at least X=100 plants
	use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_firms_with_at_least_100_plants_data.dta", clear
	rename log_pop_density_12 log_pop_density
	label variable log_pop_density "$\ln \mathcal{L}_{s}$"
	reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
	outreg2 using "Tables\TableIII.tex", label append tex(fragment) nocons ///
		ctitle("\textit{Firms with}", "\textit{$\geq 100$ plants}", "$\hspace{2em}\ln L_{j^*(s)}\hspace{2em}$") addtext(SIC8 FE, Yes, M, 12)
restore

// Industries where largest firm has at least X=100 plants
merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_in_industries_where_largest_firm_has_at_least_100_plants.dta", keep(3) nogen
reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableIII.tex", label append tex(fragment) nocons ///
	ctitle("\textit{Industries in which}", "\textit{largest firms has}", "\textit{$\geq 100$ plants}", "$\hspace{2em}\ln L_{j^*(s)}\hspace{2em}$") addtext(SIC8 FE, Yes, M, 12)


*************Table VI: The national size of the largest firm in town************
// Panel 1: Changing M
foreach m of numlist 3 6 12 24 48 {
	if `m' == 3 {
		local append_replace = "replace"
	}
	else {
		local append_replace = "append"
	}
	use "Data\Final\TablesIII_VI\largest_firm_in_town_M`m'_mean_of_ties_data.dta", clear
	rename log_pop_density_`m' log_pop_density
	label variable log_pop_density "$\ln \mathcal{L}_{s}$"
	reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
	outreg2 using "Tables\TableVI_panel1.doc", label `append_replace' tex(fragment) nocons nonotes /// 
		ctitle("$\ln L_{j^*(s)}$") addtext(SIC8 FE, Yes, M, `m')
	
	if `m' == 12 {
		outreg2 using "Tables\TableVI_panel2.doc", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8 FE, Yes, M, `m')
		outreg2 using "Tables\TableVI_panel3.doc", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8 FE, Yes, M, `m')
		outreg2 using "Tables\TableVI_panel4.doc", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}") addtext(SIC8 FE, Yes, M, `m')
		outreg2 using "Tables\TableVI_panel5.doc", label replace tex(fragment) nocons nonotes ///
			ctitle("\textit{Baseline}", " ") addtext(SIC8 FE, Yes, M, `m')
	}
}

// Panel 2: firms with at least X plants
foreach X of numlist 10 20 50 100 {
	use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_firms_with_at_least_`X'_plants_data.dta", clear
	rename log_pop_density_12 log_pop_density
	label variable log_pop_density "$\ln \mathcal{L}_{s}$"
	reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
	outreg2 using "Tables\TableVI_panel2.tex", label append tex(fragment) nocons nonotes ///
		ctitle("\textit{X = `X'}") addtext(SIC8 FE, Yes, M, 12)
}

// Panel 3: Industries where largest firm has at least X plants
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_mean_of_ties_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
foreach X of numlist 10 20 50 100 {
	preserve
		merge m:1 hq sic8 using "Data\Intermediate\NETS\NETS_HQs_at_least_5_employees_2014_hq-sic8_of_firms_in_industries_where_largest_firm_has_at_least_`X'_plants.dta", assert(1 3) keep(3) nogen
		reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
		outreg2 using "Tables\TableVI_panel3.tex", label append tex(fragment) nocons nonotes ///
			ctitle("\textit{X = `X'}") addtext(SIC8 FE, Yes, M, 12)
	restore
}

// Panel 4: By major industry
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_mean_of_ties_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
foreach div in manufacturing services retail FIRE {
	preserve
		if "`div'" == "retail" {
			keep if sic2>=52 & sic2<=59
			local divlabel = "\textit{Retail Trade}"
		}
		else if "`div'" == "services" {
			keep if sic2>=70 & sic2<=89
			local divlabel = "\textit{Services}"
		}
		else if "`div'" == "manufacturing" {
			keep if sic2>=20 & sic2<=39	
			local divlabel = "\textit{Manufacturing}"
		}
		else if "`div'" == "FIRE" {
			keep if sic2>=60 & sic2<=67
			local divlabel = "\textit{FIRE}"
		}
		reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
		outreg2 using "Tables\TableVI_panel4.tex", label append tex(fragment) nocons nonotes ///
			ctitle("`divlabel'") addtext(SIC8 FE, Yes, M, 12)
	restore
}

// Panel 5
// excluding own contribution to total firm employment
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_excluding_own_contribution_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
reghdfe log_firm_emp_net_location log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableVI_panel5.doc", label append tex(fragment) nocons nonotes ///
	ctitle("\textit{Excluding Own}", "\textit{Contribution}") addtext(SIC8 FE, Yes, M, 12)

// discarding industry-locations with ties in greatest number of plants
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_discard_ties_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableVI_panel5.doc", label append tex(fragment) nocons nonotes ///
	ctitle("\textit{Discarding}", "\textit{Ties}") addtext(SIC8 FE, Yes, M, 12)

// using maximum log firm employment among firms with greatest number of plants 
// in an industry-location
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_max_of_ties_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableVI_panel5.doc", label append tex(fragment) nocons nonotes ///
	ctitle("\textit{Largest Firm}", "\textit{Among Ties}") addtext(SIC8 FE, Yes, M, 12)

// using non-imputed data
use "Data\Final\TablesIII_VI\largest_firm_in_town_M12_nonimputed_data.dta", clear
rename log_pop_density_12 log_pop_density
label variable log_pop_density "$\ln \mathcal{L}_{s}$"
reghdfe log_firm_employment log_pop_density [aweight=industry_emp], a(sic8) vce(robust)
outreg2 using "Tables\TableVI_panel5.doc", label append tex(fragment) nocons nonotes ///
	ctitle("\textit{Non-imputed}", " ") addtext(SIC8 FE, Yes, M, 12)
